﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Security.Authentication;

namespace SemestralniPraceY35VAN
{
    public partial class MainForm : Form
    {
        Foursquare foursquare;
        Venue venue;

        /// <summary>
        /// 
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.showPreloader();

            try
            {
                this.foursquare = new Foursquare(this.tbUsername.Text, this.tbPassword.Text, this.cbDebugMode.Checked);

                this.displayUser(foursquare.User.ID);
            }
            catch (TimeoutException ex)
            {
                this.displayMessage("Timeout Exception", ex.Message);

                this.hidePreloader();
            }
            catch (AuthenticationException ex)
            {
                this.displayMessage("Unauthorized", ex.Message);

                this.hidePreloader();
            }
            catch (Exception ex)
            {
                this.displayMessage("Chyba", ex.Message);

                this.hidePreloader();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                Hide();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void systemTrayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (FormWindowState.Normal == WindowState)
                {
                    Hide();
                    WindowState = FormWindowState.Minimized;
                }
                else
                {
                    Show();
                    WindowState = FormWindowState.Normal;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pbCloseButton_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Hide();
                WindowState = FormWindowState.Minimized;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="imgToResize"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        private static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

        /// <summary>
        /// 
        /// </summary>
        private void showPreloader()
        {
            this.preloaderPanel.Visible = true;
        }

        /// <summary>
        /// 
        /// </summary>
        private void hidePreloader()
        {
            this.preloaderPanel.Invoke(new MethodInvoker(delegate { this.preloaderPanel.Visible = false; }));
        }

        /// <summary>
        /// 
        /// </summary>
        private void displayFriends()
        {
            bwCheckins.RunWorkerAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        private void postShout()
        {
            this.bwShout.RunWorkerAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        private void postCheckin()
        {
            this.bwCheckin.RunWorkerAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        private void displayPlaces()
        {
            try
            {
                if (bwPlaces.IsBusy)
                {
                    bwPlaces.CancelAsync();
                }
                bwPlaces.RunWorkerAsync();
            }
            catch (InvalidOperationException)
            { }
        }

        /// <summary>
        /// 
        /// </summary>
        private void displayUser(string uid)
        {
            bwUser.RunWorkerAsync(uid);
        }

        /// <summary>
        /// 
        /// </summary>
        private void displayVenue(string vid)
        {
            bwVenue.RunWorkerAsync(vid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwCheckins_DoWork(object sender, DoWorkEventArgs e)
        {
            this.pCheckinsInner.Invoke(new MethodInvoker(delegate { this.pCheckinsInner.Controls.Clear(); }));

            List<Checkin> checkins = foursquare.getCheckins();

            int pos = 0;
            string age = "";

            for (int i = 0; i < checkins.Count; i++)
            {
                if (age != checkins[i].Age)
                {
                    age = checkins[i].Age;

                    Label lblAge = new Label();

                    lblAge.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblAge.Image = global::SemestralniPraceY35VAN.Properties.Resources.columnheader;
                    lblAge.Location = new System.Drawing.Point(0, pos);
                    lblAge.Margin = new System.Windows.Forms.Padding(0);
                    lblAge.Name = "lblAge" + age;
                    lblAge.Size = new System.Drawing.Size(223, 22);
                    lblAge.TabIndex = 1;
                    lblAge.Text = age;
                    lblAge.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                    //
                    // pCheckinsInner
                    //
                    this.pCheckinsInner.Invoke(new MethodInvoker(delegate { this.pCheckinsInner.Controls.Add(lblAge); }));

                    pos += 22;
                }

                Panel pCheckin = new Panel();
                PictureBox pbCheckinAvatar = new PictureBox();
                Label lblCheckinDisplay = new Label();
                Label lblCheckinAddress = new Label();
                Label lblCheckinBefore = new Label();
                // 
                // pCheckin
                // 
                pCheckin.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.checkinbg;
                pCheckin.Controls.Add(lblCheckinBefore);
                pCheckin.Controls.Add(lblCheckinAddress);
                pCheckin.Controls.Add(lblCheckinDisplay);
                pCheckin.Controls.Add(pbCheckinAvatar);
                pCheckin.Location = new System.Drawing.Point(0, pos);
                pCheckin.Margin = new System.Windows.Forms.Padding(0);
                pCheckin.Name = "pCheck" + (checkins[i].Venue == null ? Convert.ToString(i) : checkins[i].Venue.ID);
                pCheckin.Size = new System.Drawing.Size(223, 47);
                pCheckin.TabIndex = 0;
                pCheckin.Cursor = System.Windows.Forms.Cursors.Hand;
                pCheckin.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                pCheckin.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                pCheckin.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                // 
                // pbCheckinAvatar
                // 
                pbCheckinAvatar.Location = new System.Drawing.Point(5, 6);
                pbCheckinAvatar.Name = "pbChAv" + (checkins[i].Venue == null ? Convert.ToString(i) : checkins[i].Venue.ID);
                pbCheckinAvatar.Size = new System.Drawing.Size(32, 32);
                pbCheckinAvatar.TabIndex = 0;
                pbCheckinAvatar.TabStop = false;
                pbCheckinAvatar.Image = resizeImage(checkins[i].User.Photo, new Size(32, 32));
                pbCheckinAvatar.Cursor = System.Windows.Forms.Cursors.Hand;
                pbCheckinAvatar.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                pbCheckinAvatar.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                pbCheckinAvatar.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                // 
                // lblCheckinDisplay
                // 
                lblCheckinDisplay.AutoSize = true;
                lblCheckinDisplay.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblCheckinDisplay.Location = new System.Drawing.Point(43, 6);
                lblCheckinDisplay.Name = "lblChD" + (checkins[i].Venue == null ? Convert.ToString(i) : checkins[i].Venue.ID);
                lblCheckinDisplay.Size = new System.Drawing.Size(95, 12);
                lblCheckinDisplay.TabIndex = 1;
                lblCheckinDisplay.Text = checkins[i].Display;
                lblCheckinDisplay.Cursor = System.Windows.Forms.Cursors.Hand;
                lblCheckinDisplay.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                lblCheckinDisplay.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                lblCheckinDisplay.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                // 
                // lblCheckinAddress
                // 
                lblCheckinAddress.AutoSize = true;
                lblCheckinAddress.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblCheckinAddress.Location = new System.Drawing.Point(43, 17);
                lblCheckinAddress.Name = "lblChA" + (checkins[i].Venue == null ? Convert.ToString(i) : checkins[i].Venue.ID);
                lblCheckinAddress.Size = new System.Drawing.Size(73, 12);
                lblCheckinAddress.TabIndex = 2;
                lblCheckinAddress.Text = checkins[i].Venue == null ? "" : checkins[i].Venue.Address;
                lblCheckinAddress.Cursor = System.Windows.Forms.Cursors.Hand;
                lblCheckinAddress.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                lblCheckinAddress.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                lblCheckinAddress.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                // 
                // lblCheckinBefore
                // 
                lblCheckinBefore.AutoSize = true;
                lblCheckinBefore.Font = new System.Drawing.Font("Verdana", 6F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblCheckinBefore.Location = new System.Drawing.Point(43, 30);
                lblCheckinBefore.Name = "lblChB" + (checkins[i].Venue == null ? Convert.ToString(i) : checkins[i].Venue.ID);
                lblCheckinBefore.Size = new System.Drawing.Size(53, 10);
                lblCheckinBefore.TabIndex = 3;
                lblCheckinBefore.Text = checkins[i].Before;
                lblCheckinBefore.Cursor = System.Windows.Forms.Cursors.Hand;
                lblCheckinBefore.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                lblCheckinBefore.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                lblCheckinBefore.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                //
                // pCheckinsInner
                //
                this.pCheckinsInner.Invoke(new MethodInvoker(delegate { this.pCheckinsInner.Controls.Add(pCheckin); }));

                pos += 47;
            }

            string html;

            html = "<html style=\"margin: 0; padding: 0;\">";
            html += "<head>";
            html += "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAWg-yy1B9LVk1Aelus5KJdRS_WJEFokBPj5l23lG9mTcFlMpIgBQbEOzFfN_hA_L0aiJXhW5O1VUpew\" type=\"text/javascript\"></script>";
            html += "<script type=\"text/javascript\">";
            html += "function initialize() {";
            html += "var gmarkers = new Array(" + checkins.Count + ");";
            html += "if (GBrowserIsCompatible()) {";
            html += "var map = new GMap2(document.getElementById(\"map_canvas\"));";
            html += "map.setCenter(new GLatLng(0,0),0);";
            html += "var bounds = new GLatLngBounds();";
            html += "var point;";
            for (int i = 0; i < checkins.Count; i++)
            {
                if (checkins[i].Venue != null)
                {
                    html += "point = new GLatLng(" + checkins[i].Venue.GeoLat + "," + checkins[i].Venue.GeoLong + ");";
                    html += "bounds.extend(point);";
                    html += "gmarkers[" + i + "] = new GMarker(point);";
                    html += "map.addOverlay(gmarkers[" + i + "]);";
                    html += "GEvent.addListener(gmarkers[" + i + "], \"click\", function() { gmarkers[" + i + "].openInfoWindowHtml(\"<table style=\\\"font-family: Verdana, 'Geneva CE', lucida, sans-serif; font-size: 7pt;\\\" cellspacing=\\\"0\\\"><tr><td rowspan=\\\"3\\\"><img src=\\\"" + checkins[i].User.Src + "\\\" width=\\\"42\\\" height=\\\"42\\\" /></td><td><strong>" + checkins[i].Display + "</strong></td></tr><tr><td>" + (checkins[i].Venue == null ? "" : checkins[i].Venue.Address) + "</td></tr><tr><td style=\\\"font-size: 6pt;\\\">" + checkins[i].Before + "</td></tr></table>\"); });";
                }
            }
            html += "map.setZoom(map.getBoundsZoomLevel(bounds) - 1);";
            html += "map.setCenter(bounds.getCenter());";
            html += "map.setUIToDefault();";
            html += "}";
            html += "}";
            html += "</script>";
            html += "</head>";
            html += "<body onload=\"initialize()\" onunload=\"GUnload()\" style=\"margin: 0; padding: 0;\">";
            html += "<div id=\"map_canvas\" style=\"width: 360px; height: 273px\" style=\"margin: 0;\"></div>";
            html += "";
            html += "</body>";
            html += "</html>";

            this.wbMap.Invoke(new MethodInvoker(delegate { this.wbMap.DocumentText = html; }));

            this.pCheckinsInner.Invoke(new MethodInvoker(delegate { this.pCheckinsInner.Height = pos - 1; }));

            this.friendsPanel.Invoke(new MethodInvoker(delegate { this.friendsPanel.Visible = true; }));

            this.preloaderPanel.Invoke(new MethodInvoker(delegate { this.preloaderPanel.Visible = false; }));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbFacebookShout_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbFacebookShout.Checked)
            {
                this.cbFacebookShout.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.facebookon;
            }
            else
            {
                this.cbFacebookShout.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.facebookoff;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbTwitterShout_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbTwitterShout.Checked)
            {
                this.cbTwitterShout.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.twitteron;
            }
            else
            {
                this.cbTwitterShout.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.twitteroff;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbShareCheckin_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbShareCheckin.Checked)
            {
                this.cbShareCheckin.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.shareyes;
            }
            else
            {
                this.cbShareCheckin.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.shareno;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbFacebookCheckin_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbFacebookCheckin.Checked)
            {
                this.cbFacebookCheckin.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.facebookon;
            }
            else
            {
                this.cbFacebookCheckin.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.facebookoff;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbTwitterCheckin_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbTwitterCheckin.Checked)
            {
                this.cbTwitterCheckin.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.twitteron;
            }
            else
            {
                this.cbTwitterCheckin.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.twitteroff;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbCheckinShout_TextChanged(object sender, EventArgs e)
        {
            int count = 140 - tbCheckinShout.Text.Length;

            this.lblCheckinCharCount.Text = Convert.ToString(count);

            if (count < 0)
            {
                lblCheckinCharCount.ForeColor = Color.DarkRed;
            }
            else
            {
                lblCheckinCharCount.ForeColor = Color.FromArgb(64, 64, 64);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbShout_TextChanged(object sender, EventArgs e)
        {
            int count = 140 - tbShout.Text.Length;

            this.lblShoutCharCount.Text = Convert.ToString(count);

            if (count < 0)
            {
                lblShoutCharCount.ForeColor = Color.DarkRed;
            }
            else
            {
                lblShoutCharCount.ForeColor = Color.FromArgb(64, 64, 64);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwPlaces_DoWork(object sender, DoWorkEventArgs e)
        {
            this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Controls.Clear(); }));

            Thread.Sleep(1000);

            Venue venue = null;

            if (foursquare.User.Checkin.Venue != null)
            {
                venue = foursquare.User.Checkin.Venue;
            }
            else
            {
                List<Checkin> checkins = foursquare.getHistory();

                foreach (Checkin checkin in checkins)
                {
                    if (checkin.Venue != null)
                    {
                        venue = checkin.Venue;
                        break;
                    }
                }
            }

            List<Group> groups;

            try
            {
                if (venue != null)
                {
                    groups = foursquare.searchVenuesNearby(venue.GeoLat, venue.GeoLong, this.tbSearch.Text);
                }
                else
                {
                    groups = foursquare.searchVenuesNearby(Convert.ToString(50.087731), Convert.ToString(14.424234), this.tbSearch.Text);
                }
            }
            catch (Exception)
            {
                return;
            }

            int pos = 41;

            for (int i = 0; i < groups.Count; i++)
            {
                Label lblType = new Label();

                lblType.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblType.Image = global::SemestralniPraceY35VAN.Properties.Resources.columnheader;
                lblType.Location = new System.Drawing.Point(0, pos);
                lblType.Margin = new System.Windows.Forms.Padding(0);
                lblType.Name = "lblType" + i;
                lblType.Size = new System.Drawing.Size(223, 22);
                lblType.TabIndex = 1;
                lblType.Text = groups[i].Type;
                lblType.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                //
                // pPlacesInner
                //
                this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Controls.Add(lblType); }));

                pos += 22;

                for (int j = 0; j < groups[i].Venues.Count; j++)
                {
                    Panel pPlace = new Panel();
                    PictureBox pbCategoryIcon = new PictureBox();
                    Label lblVenueName = new Label();
                    Label lblVenueAddress = new Label();
                    Label lblDistance = new Label();
                    // 
                    // pCheckin
                    // 
                    pPlace.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.checkinbg;
                    pPlace.Controls.Add(lblDistance);
                    pPlace.Controls.Add(lblVenueAddress);
                    pPlace.Controls.Add(lblVenueName);
                    pPlace.Controls.Add(pbCategoryIcon);
                    pPlace.Location = new System.Drawing.Point(0, pos);
                    pPlace.Margin = new System.Windows.Forms.Padding(0);
                    pPlace.Name = "pVenue" + groups[i].Venues[j].ID;
                    pPlace.Size = new System.Drawing.Size(223, 47);
                    pPlace.TabIndex = 0;
                    pPlace.Cursor = System.Windows.Forms.Cursors.Hand;
                    pPlace.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    pPlace.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    pPlace.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);

                    // 
                    // pbCategoryIcon
                    // 
                    pbCategoryIcon.Location = new System.Drawing.Point(5, 6);
                    pbCategoryIcon.Name = "pbCaIc" + groups[i].Venues[j].ID;
                    pbCategoryIcon.Size = new System.Drawing.Size(32, 32);
                    pbCategoryIcon.TabIndex = 0;
                    pbCategoryIcon.TabStop = false;
                    if (groups[i].Venues[j].PrimaryCategory != null)
                    {
                        pbCategoryIcon.Image = groups[i].Venues[j].PrimaryCategory.Icon;
                    }
                    else
                    {
                        pbCategoryIcon.Image = global::SemestralniPraceY35VAN.Properties.Resources.nocategory;
                    }
                    pbCategoryIcon.Cursor = System.Windows.Forms.Cursors.Hand;
                    pbCategoryIcon.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    pbCategoryIcon.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    pbCategoryIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblCheckinDisplay
                    // 
                    lblVenueName.AutoSize = true;
                    lblVenueName.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblVenueName.Location = new System.Drawing.Point(43, 6);
                    lblVenueName.Name = "lblVeN" + groups[i].Venues[j].ID;
                    lblVenueName.Size = new System.Drawing.Size(95, 12);
                    lblVenueName.TabIndex = 1;
                    lblVenueName.Text = groups[i].Venues[j].Name;
                    lblVenueName.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblVenueName.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblVenueName.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblVenueName.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblVenueAddress
                    // 
                    lblVenueAddress.AutoSize = true;
                    lblVenueAddress.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblVenueAddress.Location = new System.Drawing.Point(43, 17);
                    lblVenueAddress.Name = "lblVeA" + groups[i].Venues[j].ID;
                    lblVenueAddress.Size = new System.Drawing.Size(73, 12);
                    lblVenueAddress.TabIndex = 2;
                    lblVenueAddress.Text = groups[i].Venues[j].Address;
                    lblVenueAddress.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblVenueAddress.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblVenueAddress.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblVenueAddress.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblCheckinBefore
                    // 
                    lblDistance.AutoSize = true;
                    lblDistance.Font = new System.Drawing.Font("Verdana", 6F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblDistance.Location = new System.Drawing.Point(43, 30);
                    lblDistance.Name = "lblDis" + groups[i].Venues[j].ID;
                    lblDistance.Size = new System.Drawing.Size(53, 10);
                    lblDistance.TabIndex = 3;
                    lblDistance.Text = groups[i].Venues[j].Distance + " meters";
                    lblDistance.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblDistance.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblDistance.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblDistance.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    //
                    // pPlacesInner
                    //
                    this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Controls.Add(pPlace); }));

                    pos += 47;
                }
            }

            this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Height = pos - 1; }));

            string html;

            html = "<html style=\"margin: 0; padding: 0;\">";
            html += "<head>";
            html += "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAWg-yy1B9LVk1Aelus5KJdRS_WJEFokBPj5l23lG9mTcFlMpIgBQbEOzFfN_hA_L0aiJXhW5O1VUpew\" type=\"text/javascript\"></script>";
            html += "<script type=\"text/javascript\">";
            html += "function initialize() {";

            var count = 0;

            foreach (Group group in groups)
            {
                count += group.Venues.Count;
            }

            html += "var gmarkers = new Array(" + count + ");";
            html += "if (GBrowserIsCompatible()) {";
            html += "var map = new GMap2(document.getElementById(\"map_canvas\"));";
            html += "map.setCenter(new GLatLng(0,0),0);";
            html += "var bounds = new GLatLngBounds();";
            html += "var point;";

            count = 0;

            foreach (Group group in groups)
            {
                foreach (Venue ven in group.Venues)
                {
                    html += "point = new GLatLng(" + ven.GeoLat + "," + ven.GeoLong + ");";
                    html += "bounds.extend(point);";
                    html += "gmarkers[" + count + "] = new GMarker(point);";
                    html += "map.addOverlay(gmarkers[" + count + "]);";
                    html += "GEvent.addListener(gmarkers[" + count + "], \"click\", function() { gmarkers[" + count + "].openInfoWindowHtml(\"<table style=\\\"font-family: Verdana, 'Geneva CE', lucida, sans-serif; font-size: 7pt;\\\" cellspacing=\\\"0\\\"><tr><td rowspan=\\\"3\\\"><img src=\\\"" + (ven.PrimaryCategory == null ? "http://foursquare.com/img/categories/question.png" : ven.PrimaryCategory.Src) + "\\\" width=\\\"32\\\" height=\\\"32\\\" /></td><td><strong>" + ven.Name + "</strong></td></tr><tr><td>" + ven.Address + "</td></tr><tr><td style=\\\"font-size: 6pt;\\\">" + ven.Distance + " meters</td></tr></table>\"); });";

                    count++;
                }
            }
            html += "map.setZoom(map.getBoundsZoomLevel(bounds) - 1);";
            html += "map.setCenter(bounds.getCenter());";
            html += "map.setUIToDefault();";
            html += "}";
            html += "}";
            html += "</script>";
            html += "</head>";
            html += "<body onload=\"initialize()\" onunload=\"GUnload()\" style=\"margin: 0; padding: 0;\">";
            html += "<div id=\"map_canvas\" style=\"width: 360px; height: 273px\" style=\"margin: 0;\"></div>";
            html += "";
            html += "</body>";
            html += "</html>";

            this.wbPlacesMap.Invoke(new MethodInvoker(delegate { this.wbPlacesMap.DocumentText = html; }));

            this.placesPanel.Invoke(new MethodInvoker(delegate { this.placesPanel.Visible = true; }));

            this.hidePreloader();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="message"></param>
        private void displayMessage(string title, string message)
        {
            this.lblMessageTitle.Invoke(new MethodInvoker(delegate { this.lblMessageTitle.Text = title; }));
            this.lblMessageText.Invoke(new MethodInvoker(delegate { this.lblMessageText.Text = message; }));
            this.messagePanel.Invoke(new MethodInvoker(delegate { this.messagePanel.Visible = true; }));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pVenue_MouseHover(object sender, EventArgs e)
        {
            if (sender is Panel)
            {
                ((Control)sender).BackColor = Color.LightYellow;
            }
            else
            {
                ((Control)sender).Parent.BackColor = Color.LightYellow;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pVenue_MouseLeave(object sender, EventArgs e)
        {
            if (sender is Panel)
            {
                ((Control)sender).BackColor = Color.White;
            }
            else
            {
                ((Control)sender).Parent.BackColor = Color.White;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pVenue_MouseClick(object sender, MouseEventArgs e)
        {
            string vid = ((Control)sender).Name.Substring(6);

            if (vid.Length > 2)
            {
                this.showPreloader();

                try
                {
                    this.displayVenue(vid);
                }
                catch (TimeoutException ex)
                {
                    this.displayMessage("Timeout Exception", ex.Message);

                    this.hidePreloader();
                }
                catch (AuthenticationException ex)
                {
                    this.displayMessage("Unauthorized", ex.Message);

                    this.hidePreloader();
                }
                catch (Exception ex)
                {
                    this.displayMessage("Chyba", ex.Message);

                    this.hidePreloader();
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearSearch_Click(object sender, EventArgs e)
        {
            this.tbSearch.Text = "";
            this.tbSearch.Focus();
            this.btnClearSearch.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwUser_DoWork(object sender, DoWorkEventArgs e)
        {
            //
            // Mayorship
            ///

            this.pMayorshipInner.Invoke(new MethodInvoker(delegate { this.pMayorshipInner.Controls.Clear(); }));

            User user = this.foursquare.getUserDetail((string)e.Argument, true, true);

            this.lblUser.Invoke(new MethodInvoker(delegate { this.lblUser.Text = user.FirstName + " " + user.LastName; }));

            Venue ven = null;

            if (user.Checkin != null && user.Checkin.Venue != null)
            {
                ven = user.Checkin.Venue;
            }
            else
            {
                List<Checkin> history = this.foursquare.getHistory();

                foreach (Checkin checkin in history)
                {
                    if (checkin.Venue != null)
                    {
                        ven = checkin.Venue;
                    }
                }
            }

            if (ven != null)
            {
                this.lblUserLocation.Invoke(new MethodInvoker(delegate { this.lblUserLocation.Text = ven.City + ", " + ven.State; }));
            }
            else
            {
                this.lblUserLocation.Invoke(new MethodInvoker(delegate { this.lblUserLocation.Text = ""; }));
            }

            Label lblMayorship = new Label();

            // 
            // lblMayorship
            // 
            lblMayorship.BackColor = System.Drawing.Color.Transparent;
            lblMayorship.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            lblMayorship.ForeColor = System.Drawing.Color.Black;
            lblMayorship.Image = global::SemestralniPraceY35VAN.Properties.Resources.columnheader;
            lblMayorship.Location = new System.Drawing.Point(0, 0);
            lblMayorship.Name = "lblMayorship";
            lblMayorship.Size = new System.Drawing.Size(223, 22);
            lblMayorship.TabIndex = 0;
            lblMayorship.Text = "Mayorship";
            lblMayorship.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

            //
            // pMayorshipInner
            //
            this.pMayorshipInner.Invoke(new MethodInvoker(delegate { this.pMayorshipInner.Controls.Add(lblMayorship); }));

            int pos = 22;

            if (user.Mayor != null)
            {
                foreach (Venue venue in user.Mayor)
                {
                    Panel pMayorship = new Panel();
                    PictureBox pbMayorshipCategoryIcon = new PictureBox();
                    Label lblMayorshipName = new Label();
                    Label lblMayorshipAddress = new Label();
                    // 
                    // pMayorship
                    // 
                    pMayorship.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.checkinbg;
                    pMayorship.Controls.Add(lblMayorshipAddress);
                    pMayorship.Controls.Add(lblMayorshipName);
                    pMayorship.Controls.Add(pbMayorshipCategoryIcon);
                    pMayorship.Location = new System.Drawing.Point(0, pos);
                    pMayorship.Margin = new System.Windows.Forms.Padding(0);
                    pMayorship.Name = "pMayor" + venue.ID;
                    pMayorship.Size = new System.Drawing.Size(223, 47);
                    pMayorship.TabIndex = 0;
                    pMayorship.Cursor = System.Windows.Forms.Cursors.Hand;
                    pMayorship.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    pMayorship.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    pMayorship.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // pbMayorshipCategoryIcon
                    // 
                    pbMayorshipCategoryIcon.Location = new System.Drawing.Point(5, 6);
                    pbMayorshipCategoryIcon.Name = "pbMaCI" + venue.ID;
                    pbMayorshipCategoryIcon.Size = new System.Drawing.Size(32, 32);
                    pbMayorshipCategoryIcon.TabIndex = 0;
                    pbMayorshipCategoryIcon.TabStop = false;
                    if (venue.PrimaryCategory != null)
                    {
                        pbMayorshipCategoryIcon.Image = venue.PrimaryCategory.Icon;
                    }
                    else
                    {
                        pbMayorshipCategoryIcon.Image = global::SemestralniPraceY35VAN.Properties.Resources.nocategory;
                    }
                    pbMayorshipCategoryIcon.Cursor = System.Windows.Forms.Cursors.Hand;
                    pbMayorshipCategoryIcon.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    pbMayorshipCategoryIcon.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    pbMayorshipCategoryIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblMayorshipName
                    // 
                    lblMayorshipName.AutoSize = true;
                    lblMayorshipName.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblMayorshipName.Location = new System.Drawing.Point(43, 6);
                    lblMayorshipName.Name = "lblMaN" + venue.ID;
                    lblMayorshipName.Size = new System.Drawing.Size(95, 12);
                    lblMayorshipName.TabIndex = 1;
                    lblMayorshipName.Text = venue.Name;
                    lblMayorshipName.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblMayorshipName.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblMayorshipName.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblMayorshipName.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblMayorshipAddress
                    // 
                    lblMayorshipAddress.AutoSize = true;
                    lblMayorshipAddress.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblMayorshipAddress.Location = new System.Drawing.Point(43, 17);
                    lblMayorshipAddress.Name = "lblMaA" + venue.ID;
                    lblMayorshipAddress.Size = new System.Drawing.Size(73, 12);
                    lblMayorshipAddress.TabIndex = 2;
                    lblMayorshipAddress.Text = venue.Address;
                    lblMayorshipAddress.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblMayorshipAddress.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblMayorshipAddress.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblMayorshipAddress.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    //
                    // pMayorshipInner
                    //
                    this.pMayorshipInner.Invoke(new MethodInvoker(delegate { this.pMayorshipInner.Controls.Add(pMayorship); }));

                    pos += 47;
                }
            }

            this.pMayorshipInner.Invoke(new MethodInvoker(delegate { this.pMayorshipInner.Height = pos - 1; }));

            this.pBadgesInner.Invoke(new MethodInvoker(delegate { this.pBadgesInner.Controls.Clear(); }));

            int posx = 9;
            int posy = 9;

            if (user.Badges != null)
            {
                foreach (Badge badge in user.Badges)
                {
                    PictureBox pbBadge = new PictureBox();
                    // 
                    // pbMayorshipCategoryIcon
                    // 
                    pbBadge.Location = new System.Drawing.Point(posx, posy);
                    pbBadge.Name = "pbBadge" + badge.ID;
                    pbBadge.Size = new System.Drawing.Size(57, 57);
                    pbBadge.TabIndex = 0;
                    pbBadge.TabStop = false;
                    pbBadge.Image = badge.Icon;
                    //
                    // pMayorshipInner
                    //
                    this.pBadgesInner.Invoke(new MethodInvoker(delegate { this.pBadgesInner.Controls.Add(pbBadge); }));

                    if (posx < 268)
                    {
                        posx += 67;
                    }
                    else
                    {
                        posx = 9;
                        posy += 67;
                    }
                }
            }

            this.pBadgesInner.Invoke(new MethodInvoker(delegate { this.pBadgesInner.Height = posx == 9 ? posy : posy + 67; }));

            this.userPanel.Invoke(new MethodInvoker(delegate { this.userPanel.Visible = true; }));

            this.hidePreloader();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwVenue_DoWork(object sender, DoWorkEventArgs e)
        {
            string vid = (string)e.Argument;

            //this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Controls.Clear(); }));
            
            this.venue = foursquare.getVenueDetails(vid);

            this.lblVenueName.Invoke(new MethodInvoker(delegate { this.lblVenueName.Text = this.venue.Name; }));
            this.lblVeneuAddress.Invoke(new MethodInvoker(delegate { this.lblVeneuAddress.Text = this.venue.Address; }));

            /*
            int pos = 41;

            for (int i = 0; i < groups.Count; i++)
            {
                Label lblType = new Label();

                lblType.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblType.Image = global::SemestralniPraceY35VAN.Properties.Resources.columnheader;
                lblType.Location = new System.Drawing.Point(0, pos);
                lblType.Margin = new System.Windows.Forms.Padding(0);
                lblType.Name = "lblType" + i;
                lblType.Size = new System.Drawing.Size(223, 22);
                lblType.TabIndex = 1;
                lblType.Text = groups[i].Type;
                lblType.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                //
                // pPlacesInner
                //
                this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Controls.Add(lblType); }));

                pos += 22;

                for (int j = 0; j < groups[i].Venues.Count; j++)
                {
                    Panel pPlace = new Panel();
                    PictureBox pbCategoryIcon = new PictureBox();
                    Label lblVenueName = new Label();
                    Label lblVenueAddress = new Label();
                    Label lblDistance = new Label();
                    // 
                    // pCheckin
                    // 
                    pPlace.BackgroundImage = global::SemestralniPraceY35VAN.Properties.Resources.checkinbg;
                    pPlace.Controls.Add(lblDistance);
                    pPlace.Controls.Add(lblVenueAddress);
                    pPlace.Controls.Add(lblVenueName);
                    pPlace.Controls.Add(pbCategoryIcon);
                    pPlace.Location = new System.Drawing.Point(0, pos);
                    pPlace.Margin = new System.Windows.Forms.Padding(0);
                    pPlace.Name = "pVenue" + groups[i].Venues[j].ID;
                    pPlace.Size = new System.Drawing.Size(223, 47);
                    pPlace.TabIndex = 0;
                    pPlace.Cursor = System.Windows.Forms.Cursors.Hand;
                    pPlace.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    pPlace.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    pPlace.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);

                    // 
                    // pbCategoryIcon
                    // 
                    pbCategoryIcon.Location = new System.Drawing.Point(5, 6);
                    pbCategoryIcon.Name = "pbCaIc" + groups[i].Venues[j].ID;
                    pbCategoryIcon.Size = new System.Drawing.Size(32, 32);
                    pbCategoryIcon.TabIndex = 0;
                    pbCategoryIcon.TabStop = false;
                    if (groups[i].Venues[j].PrimaryCategory != null)
                    {
                        pbCategoryIcon.Image = groups[i].Venues[j].PrimaryCategory.Icon;
                    }
                    else
                    {
                        pbCategoryIcon.Image = global::SemestralniPraceY35VAN.Properties.Resources.nocategory;
                    }
                    pbCategoryIcon.Cursor = System.Windows.Forms.Cursors.Hand;
                    pbCategoryIcon.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    pbCategoryIcon.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    pbCategoryIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblCheckinDisplay
                    // 
                    lblVenueName.AutoSize = true;
                    lblVenueName.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblVenueName.Location = new System.Drawing.Point(43, 6);
                    lblVenueName.Name = "lblVeN" + groups[i].Venues[j].ID;
                    lblVenueName.Size = new System.Drawing.Size(95, 12);
                    lblVenueName.TabIndex = 1;
                    lblVenueName.Text = groups[i].Venues[j].Name;
                    lblVenueName.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblVenueName.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblVenueName.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblVenueName.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblVenueAddress
                    // 
                    lblVenueAddress.AutoSize = true;
                    lblVenueAddress.Font = new System.Drawing.Font("Verdana", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblVenueAddress.Location = new System.Drawing.Point(43, 17);
                    lblVenueAddress.Name = "lblVeA" + groups[i].Venues[j].ID;
                    lblVenueAddress.Size = new System.Drawing.Size(73, 12);
                    lblVenueAddress.TabIndex = 2;
                    lblVenueAddress.Text = groups[i].Venues[j].Address;
                    lblVenueAddress.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblVenueAddress.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblVenueAddress.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblVenueAddress.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    // 
                    // lblCheckinBefore
                    // 
                    lblDistance.AutoSize = true;
                    lblDistance.Font = new System.Drawing.Font("Verdana", 6F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    lblDistance.Location = new System.Drawing.Point(43, 30);
                    lblDistance.Name = "lblDis" + groups[i].Venues[j].ID;
                    lblDistance.Size = new System.Drawing.Size(53, 10);
                    lblDistance.TabIndex = 3;
                    lblDistance.Text = groups[i].Venues[j].Distance + " meters";
                    lblDistance.Cursor = System.Windows.Forms.Cursors.Hand;
                    lblDistance.MouseHover += new System.EventHandler(this.pVenue_MouseHover);
                    lblDistance.MouseLeave += new System.EventHandler(this.pVenue_MouseLeave);
                    lblDistance.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pVenue_MouseClick);
                    //
                    // pPlacesInner
                    //
                    this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Controls.Add(pPlace); }));

                    pos += 47;
                }
            }

            this.pPlacesInner.Invoke(new MethodInvoker(delegate { this.pPlacesInner.Height = pos - 1; }));
            */

            string html;

            html = "<html style=\"margin: 0; padding: 0;\">";
            html += "<head>";
            html += "<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAWg-yy1B9LVk1Aelus5KJdRS_WJEFokBPj5l23lG9mTcFlMpIgBQbEOzFfN_hA_L0aiJXhW5O1VUpew\" type=\"text/javascript\"></script>";
            html += "<script type=\"text/javascript\">";
            html += "function initialize() {";
            html += "if (GBrowserIsCompatible()) {";
            html += "var map = new GMap2(document.getElementById(\"map_canvas\"));";
            html += "map.setCenter(new GLatLng(0,0),0);";
            html += "var bounds = new GLatLngBounds();";
            html += "var point;";
            html += "point = new GLatLng(" + this.venue.GeoLat + "," + this.venue.GeoLong + ");";
            html += "bounds.extend(point);";
            html += "var marker = new GMarker(point);";
            html += "map.addOverlay(marker);";
            html += "GEvent.addListener(marker, \"click\", function() { marker.openInfoWindowHtml(\"<table style=\\\"font-family: Verdana, 'Geneva CE', lucida, sans-serif; font-size: 7pt;\\\" cellspacing=\\\"0\\\"><tr><td rowspan=\\\"3\\\"><img src=\\\"" + (this.venue.PrimaryCategory == null ? "http://foursquare.com/img/categories/question.png" : this.venue.PrimaryCategory.Src) + "\\\" width=\\\"32\\\" height=\\\"32\\\" /></td><td><strong>" + this.venue.Name + "</strong></td></tr><tr><td>" + this.venue.Address + "</td></tr><tr><td style=\\\"font-size: 6pt;\\\">" + this.venue.Distance + " meters</td></tr></table>\"); });";
            html += "map.setZoom(map.getBoundsZoomLevel(bounds) - 1);";
            html += "map.setCenter(bounds.getCenter());";
            html += "map.setUIToDefault();";
            html += "}";
            html += "}";
            html += "</script>";
            html += "</head>";
            html += "<body onload=\"initialize()\" onunload=\"GUnload()\" style=\"margin: 0; padding: 0;\">";
            html += "<div id=\"map_canvas\" style=\"width: 300px; height: 202px\" style=\"margin: 0;\"></div>";
            html += "";
            html += "</body>";
            html += "</html>";

            this.wbVenueMap.Invoke(new MethodInvoker(delegate { this.wbVenueMap.DocumentText = html; }));

            this.venuePanel.Invoke(new MethodInvoker(delegate { this.venuePanel.Visible = true; }));

            this.hidePreloader();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackMessage_Click(object sender, EventArgs e)
        {
            this.messagePanel.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackPlaces_Click(object sender, EventArgs e)
        {
            this.placesPanel.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFriends_Click(object sender, EventArgs e)
        {
            this.showPreloader();

            try
            {
                this.displayFriends();
            }
            catch (TimeoutException ex)
            {
                this.displayMessage("Timeout Exception", ex.Message);

                this.hidePreloader();
            }
            catch (AuthenticationException ex)
            {
                this.displayMessage("Unauthorized", ex.Message);

                this.hidePreloader();
            }
            catch (Exception ex)
            {
                this.displayMessage("Chyba", ex.Message);

                this.hidePreloader();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPlaces_Click(object sender, EventArgs e)
        {
            this.showPreloader();

            try
            {
                this.displayPlaces();
            }
            catch (TimeoutException ex)
            {
                this.displayMessage("Timeout Exception", ex.Message);

                this.hidePreloader();
            }
            catch (AuthenticationException ex)
            {
                this.displayMessage("Unauthorized", ex.Message);

                this.hidePreloader();
            }
            catch (Exception ex)
            {
                this.displayMessage("Chyba", ex.Message);

                this.hidePreloader();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShout_Click(object sender, EventArgs e)
        {
            this.tbShout.Text = "";
            this.lblShoutCharCount.Text = "140";

            this.shoutPanel.Visible = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbSearch_TextChanged(object sender, EventArgs e)
        {
            if (this.tbSearch.Text.Length > 0)
            {
                this.btnClearSearch.Visible = true;
            }
            this.displayPlaces();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackFriends_Click(object sender, EventArgs e)
        {
            this.friendsPanel.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackShout_Click(object sender, EventArgs e)
        {
            this.shoutPanel.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackCheckin_Click(object sender, EventArgs e)
        {
            this.checkinPanel.Visible = false;
        }

        /// <summary>
        /// Allows you to check-in to a place.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckInHere_Click(object sender, EventArgs e)
        {
            this.showPreloader();

            try
            {
                this.postCheckin();
            }
            catch (TimeoutException ex)
            {
                this.displayMessage("Timeout Exception", ex.Message);

                this.hidePreloader();
            }
            catch (AuthenticationException ex)
            {
                this.displayMessage("Unauthorized", ex.Message);

                this.hidePreloader();
            }
            catch (Exception ex)
            {
                this.displayMessage("Chyba", ex.Message);

                this.hidePreloader();
            }
        }

        /// <summary>
        /// Allows you to shout.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShoutIt_Click(object sender, EventArgs e)
        {
            this.showPreloader();

            try
            {
                this.postShout();
            }
            catch (TimeoutException ex)
            {
                this.displayMessage("Timeout Exception", ex.Message);

                this.hidePreloader();
            }
            catch (AuthenticationException ex)
            {
                this.displayMessage("Unauthorized", ex.Message);

                this.hidePreloader();
            }
            catch (Exception ex)
            {
                this.displayMessage("Chyba", ex.Message);

                this.hidePreloader();
            }
        }

        /// <summary>
        /// Shows check-in panel.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnVenueCheckin_Click(object sender, EventArgs e)
        {
            this.lblCheckinName.Text = this.venue.Name;
            this.lblCheckinAddress.Text = this.venue.Address;
            this.tbCheckinShout.Text = "";
            this.lblCheckinCharCount.Text = "140";

            this.checkinPanel.Visible = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackResult_Click(object sender, EventArgs e)
        {
            this.checkinPanel.Visible = false;
            this.shoutPanel.Visible = false;
            this.venuePanel.Visible = false;
            this.placesPanel.Visible = false;
            this.friendsPanel.Visible = false;
            this.resultPanel.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnVenueBack_Click(object sender, EventArgs e)
        {
            this.venuePanel.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwShout_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Checkin checkin = this.foursquare.shout(this.tbShout.Text, this.cbTwitterShout.Checked, this.cbFacebookShout.Checked);

                this.lblResultName.Invoke(new MethodInvoker(delegate { this.lblResultName.Text = "Shout @ Twitter && Facebook"; }));

                this.lblResultAddress.Invoke(new MethodInvoker(delegate { this.lblResultAddress.Text = "140 characters to yell at your peeps!"; }));

                this.tbResultMessage.Invoke(new MethodInvoker(delegate { this.tbResultMessage.Text = checkin.Message; }));

                this.resultPanel.Invoke(new MethodInvoker(delegate { this.resultPanel.Visible = true; }));

                this.shoutPanel.Invoke(new MethodInvoker(delegate { this.shoutPanel.Visible = false; }));

                this.preloaderPanel.Invoke(new MethodInvoker(delegate { this.preloaderPanel.Visible = false; }));
            }
            catch (IOException ex)
            {
                this.hidePreloader();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwCheckin_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Checkin checkin = this.foursquare.checkin(this.venue.ID, this.tbCheckinShout.Text, !this.cbShareCheckin.Checked, this.cbTwitterCheckin.Checked, this.cbFacebookCheckin.Checked, null, null);

                this.lblResultName.Invoke(new MethodInvoker(delegate { this.lblResultName.Text = this.venue.Name; }));

                this.lblResultAddress.Invoke(new MethodInvoker(delegate { this.lblResultAddress.Text = this.venue.Address; }));

                this.tbResultMessage.Invoke(new MethodInvoker(delegate { this.tbResultMessage.Text = checkin.Message; }));

                this.resultPanel.Invoke(new MethodInvoker(delegate { this.resultPanel.Visible = true; }));

                this.checkinPanel.Invoke(new MethodInvoker(delegate { this.checkinPanel.Visible = false; }));

                this.preloaderPanel.Invoke(new MethodInvoker(delegate { this.preloaderPanel.Visible = false; }));
            }
            catch (IOException ex)
            {
                this.hidePreloader();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogout_Click(object sender, EventArgs e)
        {
            this.userPanel.Visible = false;
        }
    }
}
